using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.CommerceServer.Catalog;

public partial class RssSubscriptionPage : System.Web.UI.Page
{
	private RssUrlBuilder rssUrlBuilder;
	private static int? maxNewItems;

	private static int MaxNewItems
	{
		get
		{
			if (RssSubscriptionPage.maxNewItems == null)
			{
				RssConfiguration settings = (RssConfiguration)HttpContext.Current.GetSection("rssConfiguration");
				RssSubscriptionPage.maxNewItems = int.Parse(settings.MaxNewItems);
			}
			return RssSubscriptionPage.maxNewItems.Value;
		}
	}

	private RssUrlBuilder RssUrlBuilder
	{
		get
		{
			if (this.rssUrlBuilder == null)
			{
				this.rssUrlBuilder = new RssUrlBuilder();
			}
			return this.rssUrlBuilder;
		}
	}
	
	private void SetupFeedUrl()
	{
		if (string.IsNullOrEmpty(this.RssUrlBuilder.ExternalFeedUrl))
		{
			this.feedUrlLink.Visible = false;
		}
		else
		{
			this.feedUrlLink.NavigateUrl = this.RssUrlBuilder.ExternalFeedUrl;
			this.feedUrlLink.Visible = true;
		}
	}

	private void SetupNewItemsDropDown()
	{
		this.newItemsNumberDropDown.Items.Clear();
		if (RssSubscriptionPage.MaxNewItems > 0)
		{
			for (int currentIndex = 0; currentIndex <= RssSubscriptionPage.MaxNewItems; currentIndex++)
			{
				this.newItemsNumberDropDown.Items.Add(new ListItem(currentIndex.ToString()));
				if (this.RssUrlBuilder.FeedConfig.NewItems == currentIndex)
				{
					this.newItemsNumberDropDown.SelectedIndex = currentIndex;
				}
			}
		}
		else
		{
			this.trackNewItemsLabel.Visible = false;
			this.newItemsNumberDropDown.Visible = false;
			this.newItemsNumberDropDown.Items.Add(new ListItem("0"));
			this.newItemsNumberDropDown.SelectedIndex = 0;
		}
	}

	private void SetupRemoveItemsGridView()
	{
		if (this.RssUrlBuilder.FeedConfig.ConfigItems.Count == 0)
		{
			this.currentItemsLabel.Text = "There are no items currently in your feed.";
			this.currentItemsGridView.Visible = false;
			this.removeButton.Visible = false;
		}
		else
		{
			this.currentItemsLabel.Text = "The following items are in your feed:";
			// construct a data table from our configuration
			DataTable configItemsTable = new DataTable("FeedItems");
			// create columns
			configItemsTable.Columns.Add("CatalogName", typeof(string));
			configItemsTable.Columns.Add("ProductID", typeof(string));
			configItemsTable.Columns.Add("Type", typeof(string));

			foreach (RssConfigItem configItem in this.RssUrlBuilder.FeedConfig.ConfigItems)
			{
				configItemsTable.Rows.Add(new string[] { configItem.Catalog, configItem.ProductID, configItem.Type });
			}
			this.currentItemsGridView.DataSource = configItemsTable;
			this.currentItemsGridView.DataBind();
			this.currentItemsGridView.Visible = true;
			this.removeButton.Visible = true;
		}
	}

	private void SetupAddItemsGridView(bool isSearchButtonClick, CatalogItemsDataSet catalogItems)
	{
		this.addItemsGridView.DataSource = catalogItems;
		this.addItemsGridView.DataBind();

		if (this.addItemsGridView.Rows.Count != 0)
		{
			this.addItemsGridView.HeaderRow.Cells[5].ToolTip = "Always include this item in my feed.";
			this.addItemsGridView.HeaderRow.Cells[6].ToolTip = "Include this item in my feed when it is on sale.";
			this.addItemsGridView.HeaderRow.Cells[7].ToolTip = "Include this item in my feed when it is in stock.";

			this.noResultsLabel.Visible = false;
			this.addButton.Visible = true;
		}
		else
		{
			if (isSearchButtonClick)
			{
				this.noResultsLabel.Visible = true;
			}
			else
			{
				this.noResultsLabel.Visible = false;
			}
			this.addButton.Visible = false;
		}

	}

    protected void Page_Load(object sender, EventArgs e)
    {
		if (Page.IsPostBack)
		{
			return;
		}

		this.SetupFeedUrl();
		this.SetupNewItemsDropDown();
		this.SetupRemoveItemsGridView();
		this.SetupAddItemsGridView(false, null);
    }

	protected void removeButton_Click(object sender, EventArgs e)
	{
		foreach (GridViewRow row in this.currentItemsGridView.Rows)
		{
			bool toRemove = ((CheckBox)row.FindControl("removeItemCheckBox")).Checked;
			if (toRemove)
			{
				RssConfigItem itemToRemove = new RssConfigItem(row.Cells[0].Text,
					row.Cells[1].Text,
					row.Cells[2].Text);
				this.RssUrlBuilder.FeedConfig.RemoveConfigItem(itemToRemove);
			}
		}
		this.RssUrlBuilder.Save();
		this.SetupRemoveItemsGridView();
	}
	
	protected void addButton_Click(object sender, EventArgs e)
	{
		const int catalogNameColumn = 0; 
		const int productIdColumn = 1;
		
		foreach (GridViewRow row in this.addItemsGridView.Rows)
		{
			string catalogName = row.Cells[catalogNameColumn].Text;
			string productId = row.Cells[productIdColumn].Text;

			bool alwaysTrack = ((CheckBox)row.FindControl("trackItem")).Checked;
			bool trackSales = ((CheckBox)row.FindControl("itemOnSale")).Checked;
			bool trackInStock = ((CheckBox)row.FindControl("itemInStock")).Checked;
			if (alwaysTrack)
			{
				this.RssUrlBuilder.WatchItem(catalogName, productId);
			}
			else if (trackSales)
			{
				this.RssUrlBuilder.WatchForSale(catalogName, productId);
			}
			else if (trackInStock)
			{
				this.RssUrlBuilder.WatchForInStock(catalogName, productId);
			}
		}

		this.RssUrlBuilder.Save();
		this.SetupRemoveItemsGridView();
	}

	protected void newItemsNumberDropDown_SelectedIndexChanged(object sender, EventArgs e)
	{
		this.RssUrlBuilder.FeedConfig.NewItems = this.newItemsNumberDropDown.SelectedIndex;
		this.RssUrlBuilder.Save();
	}
	
	protected void searchButton_Click(object sender, EventArgs e)
	{
		CatalogItemsDataSet catalogItems = null;
		string searchText = this.searchTextBox.Text;
		if (!string.IsNullOrEmpty(searchText))
		{
			catalogItems = this.RssUrlBuilder.SearchByKeyword(searchText);
			if (catalogItems != null)
			{
				this.addItemsGridView.DataSource = catalogItems.CatalogItems;
			}
		}

		this.SetupAddItemsGridView(true, catalogItems);
	}
}
